var area = require('../../data/area')
var p = 0,
  c = 0,
  d = 0
Page({
  data: {
    fromFlage:"",
    tip:1,
    provinceSelIndex: -999,
    citySelIndex: '',
    districtSelIndex: '',
    userCity: '点击选择',
    //信息相关
    name: '',
    mob: '',
    detalis: '',
    textareaDis : false,
    pageFrom : ""
  },
  onLoad: function (options) {
    console.log(options.pageFrom)
    this.setData({
      fromFlage: wx.getStorageSync('fromFlage'),
      pageFrom: options.pageFrom
    })
    // 初始化数据
    this.setAreaData()
  },
  setAreaData: function (p, c, d) {
    var p = p || 0 // provinceSelIndex
    var c = c || 0 // citySelIndex
    var d = d || 0 // districtSelIndex
    // 设置省的数据
    var province = area['100000']
    var provinceName = [];
    var provinceCode = [];
    for (var item in province) {
      provinceName.push(province[item])
      provinceCode.push(item)
    }
    this.setData({
      provinceName: provinceName,
      provinceCode: provinceCode
    })
    // 设置市的数据
    var city = area[provinceCode[p]]
    var cityName = [];
    var cityCode = [];
    for (var item in city) {
      cityName.push(city[item])
      cityCode.push(item)
    }
    this.setData({
      cityName: cityName,
      cityCode: cityCode
    })
    // 设置区的数据
    var district = area[cityCode[c]]
    var districtName = [];
    var districtCode = [];
    for (var item in district) {
      districtName.push(district[item])
      districtCode.push(item)
    }
    this.setData({
      districtName: districtName,
      districtCode: districtCode
    })
  },
  changeArea: function (e) {
    p = e.detail.value[0]
    c = e.detail.value[1]
    d = e.detail.value[2]
    this.setAreaData(p, c, d)
  },
  showDistpicker: function () {
    this.setData({
      showDistpicker: true,
      textareaDis:true
    })
  },
  distpickerCancel: function () {
    this.setData({
      showDistpicker: false,
      textareaDis: false
    })
  },
  distpickerSure: function () {
    this.setData({
      provinceSelIndex: p,
      citySelIndex: c,
      districtSelIndex: d,
      userCity: this.data.provinceName[p] + this.data.cityName[c] + this.data.districtName[d]
    })
    this.distpickerCancel()
  },
  onShow: function () {
    this.fillInfor();
    this.showLocation();
  },
  getname(e) {
    var name = e.detail.value;
    this.setData({
      name: name
    })
  },
  getMob(e) {
    var mob = e.detail.value;
    this.setData({
      mob: mob
    })
  },
  getDetalis(e) {
    var detalis = e.detail.value;
    this.setData({
      detalis: detalis
    })
  },
  fillInfor() {
    var oldAddressInfor = wx.getStorageSync('addressInfo');
    if (oldAddressInfor != "") {
      this.setData({
        name: oldAddressInfor.consignee,
        mob: oldAddressInfor.mobile,
        detalis: oldAddressInfor.add_detail,
        userCity: oldAddressInfor.area,
      })
    }

  },
  showLocation() {
    var location = wx.getStorageSync('a_a_locatin');
    var locationArr = location.split(',');
    var locationName = wx.getStorageSync('a_a_name');
    if (locationName == "") {
      locationName = '点击选择';
    }
    this.setData({
      locationName: locationName
    })
  },
  saveAddress() {
    var consignee = this.data.name;
    var mobile = this.data.mob;
    var add_detail = this.data.detalis;
    var through = wx.getStorageSync('a_a_locatin').split(',')[0];
    var weft = wx.getStorageSync('a_a_locatin').split(',')[1];
    var area = this.data.userCity;
    var position = wx.getStorageSync('a_a_name')
    var addresstype = 'r';
    console.log(area)
    if (consignee == "" || consignee == "undefined" || mobile == "" || mobile == "undefined" || add_detail == "" || add_detail == "undefined" || position == "" || position == "undefined" || area == "点击选择" || area == "undefined") {
      wx.showToast({
        title: '请填入完整信息',
        icon: 'none'
      })
    } else {
      var telRule = /^1[3|4|5|7|8]\d{9}$/,
        nameRule = /^[\u2E80-\u9FFF]+$/;
      if (!telRule.test(mobile)) {
        wx.showToast({
          title: '手机号码格式不正确',
          icon: 'none'
        })
      } else {
        var body = {};
        var url = getApp().data.url + 'userexpand/postAddress';
        body.consignee = consignee;
        body.mobile = mobile;
        body.add_detail = add_detail;
        body.through = through;
        body.weft = weft;
        body.area = area;
        body.position = position;
        body.addresstype = addresstype;
        console.log(body);
        wx.showLoading({
          icon: 'loading',
          duration: 2000
        })
        getApp().wxAjax(this.addressSug, url, body);
      }
    }
  },
  addressSug(res) {
    var resObj = res.data;
    console.log(resObj)
    wx.hideToast()
    if (resObj.header.res_code == 0) {
      wx.removeStorageSync("addressInfo");
      wx.removeStorageSync("a_a_name");
      wx.removeStorageSync("a_a_locatin");
      wx.removeStorageSync("fromFlage");
      console.log(resObj)
      var urlstr;
      if (this.data.fromFlage == 'index'){
        urlstr = '../index/index';
      }else{
        urlstr = '../addressList/addressList?form=' + this.data.pageFrom ;
      }
      var selectLoction = {};
      selectLoction.address_id = resObj.body.addressInfo.address_id;
      selectLoction.consignee = resObj.body.addressInfo.consignee;
      selectLoction.mobile = resObj.body.addressInfo.mobile;
      selectLoction.add_detail = resObj.body.addressInfo.add_detail;
      selectLoction.position = resObj.body.addressInfo.position;
      selectLoction.area = resObj.body.addressInfo.area;
      selectLoction.through = resObj.body.addressInfo.through;
      selectLoction.weft = resObj.body.addressInfo.weft;
      wx.setStorageSync('selectLoction', selectLoction);
      wx.redirectTo({
        url: urlstr
      })
    }else{
      wx.showToast({
        title: resObj.header.message,
        icon:'none'
      })
    }
  },
  toAddressList() {
    console.log(this.data.pageFrom)
    wx.navigateTo({
      url: '../addressList/addressList?form=' + this.data.pageFrom ,
    })
  },
  toGetLocation() {
    var consignee = this.data.name;
    var mobile = this.data.mob;
    var add_detail = this.data.detalis;
    var area = this.data.userCity;
    var body = {};
    body.consignee = consignee;
    body.mobile = mobile;
    body.add_detail = add_detail;
    body.area = area;
    wx.setStorageSync('addressInfo', body)
    wx.navigateTo({
      url: '../sLocation/sLocation?pageFrom=' + this.data.pageFrom,
    })
  },
  hideTip(){
    this.setData({
      tip:2
    })
  }
})